<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>香港疫情实时监控大屏</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #0f1c3c;
            color: #fff;
            font-family: "Microsoft YaHei", sans-serif;
            overflow: hidden;
        }
        .dashboard {
            display: grid;
            grid-template-columns: 70% 30%;
            grid-template-rows: 80px 1fr;
            height: 100vh;
        }
        .header {
            grid-column: 1 / span 2;
            background: linear-gradient(90deg, #1a2980, #26d0ce);
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.5);
        }
        .map-container {
            padding: 20px;
        }
        .side-panel {
            padding: 20px;
            display: grid;
            grid-template-rows: auto auto;
            gap: 20px;
        }
        .chart-container {
            background: rgba(15, 28, 60, 0.7);
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
        }
        #map {
            width: 100%;
            height: calc(100vh - 100px);
        }
        #bar-chart, #data-table {
            width: 100%;
            height: 300px;
        }
        h1 {
            margin: 0;
            font-size: 28px;
            letter-spacing: 2px;
        }
    </style>
</head>
<body>
    <div class="dashboard">
        <div class="header">
            <h1>香港疫情实时监控大屏</h1>
        </div>
        
        <div class="map-container chart-container">
            <div id="map"></div>
        </div>
        
        <div class="side-panel">
            <div class="chart-container">
                <div id="bar-chart"></div>
            </div>
            <div class="chart-container">
                <div id="data-table"></div>
            </div>
        </div>
    </div>

    <script>
        // 初始化图表
        const mapChart = echarts.init(document.getElementById('map'));
        const barChart = echarts.init(document.getElementById('bar-chart'));
        
        // 注册香港地图
        $.get('/static/js/hongkong.geojson', function(hongkongJson) {
            echarts.registerMap('HK', hongkongJson);
            fetchData();
        });

        // 定时刷新数据(每30秒)
        setInterval(fetchData, 30000);

        function fetchData() {
            $.get('/api/data', function(data) {
                updateCharts(data);
            });
        }

        function updateCharts(data) {
            // 准备地图数据
            const mapData = Object.keys(data).map(region => {
                return { name: region, value: data[region] };
            });

            // 地图配置
            const mapOption = {
                backgroundColor: 'transparent',
                title: {
                    text: '香港各区确诊分布',
                    left: 'center',
                    textStyle: { color: '#fff' }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: params => {
                        return `${params.name}<br/>累计确诊: ${params.value || 0}例`;
                    }
                },
                visualMap: {
                    min: 0,
                    max: Math.max(...Object.values(data)),
                    text: ['高', '低'],
                    realtime: false,
                    calculable: true,
                    inRange: {
                        color: ['#50a3ba', '#eac736', '#d94e5d']
                    },
                    textStyle: { color: '#fff' }
                },
                series: [{
                    name: '确诊数',
                    type: 'map',
                    map: 'HK',
                    roam: true,
                    emphasis: {
                        label: { show: true }
                    },
                    data: mapData,
                    itemStyle: {
                        areaColor: '#1a2980',
                        borderColor: '#26d0ce'
                    }
                }]
            };

            // 柱状图配置
            const barOption = {
                backgroundColor: 'transparent',
                title: {
                    text: '各区累计确诊TOP5',
                    textStyle: { color: '#fff' }
                },
                tooltip: {},
                xAxis: {
                    type: 'category',
                    data: Object.keys(data).sort((a,b) => data[b]-data[a]).slice(0,5),
                    axisLabel: { color: '#fff' }
                },
                yAxis: {
                    type: 'value',
                    axisLabel: { color: '#fff' }
                },
                series: [{
                    name: '确诊数',
                    type: 'bar',
                    data: Object.keys(data).sort((a,b) => data[b]-data[a]).slice(0,5).map(region => data[region]),
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#83bff6' },
                            { offset: 0.5, color: '#188df0' },
                            { offset: 1, color: '#0078ff' }
                        ])
                    }
                }]
            };

            mapChart.setOption(mapOption);
            barChart.setOption(barOption);
        }

        // 响应窗口大小变化
        window.addEventListener('resize', function() {
            mapChart.resize();
            barChart.resize();
        });
    </script>
</body>
</html>